package com.example.framework;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

public class BaseHelperWithWebDriver {
	protected ApplicationManager manager;
	protected WebDriver driver; 
	
	public BaseHelperWithWebDriver(ApplicationManager manager) {
		this.manager = manager;
		driver = manager.getSeleniumHelper().getSelenium();
	}
	
	protected boolean isElementPresent(By locator) {
		driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		boolean isElementPresent = driver.findElements(locator).size() > 0;
		driver.manage().timeouts().implicitlyWait(Integer.parseInt(manager.getProperties("implicitWait", "30")), TimeUnit.SECONDS);
		return isElementPresent;
	}
	
	protected void type(By locator, String text) {
		driver.findElement(locator).clear();
		driver.findElement(locator).sendKeys(text);
	}
	
	protected WebElement findElement(By locator) {
		WebElement element = driver.findElement(locator);
		return element;
	}
	
	protected List<WebElement> findElements(By locator) {
		return driver.findElements(locator);
	}
	
	protected void openUrl(String url) {
		driver.get(url);
	}
	
	protected void click(By locator) {
		findElement(locator).click();
	}
	
	protected void select(By locator, String optionVisibleTextOrIndex) {
			new Select(driver.findElement(locator)).selectByVisibleText(optionVisibleTextOrIndex);
	}
	protected String getText(By locator) {
		return findElement(locator).getText();
	}
	
	protected String getCurrentUrl() {
		return driver.getCurrentUrl();
	}
	
}
